package org.example.日期与时间;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * jdk 1~7
 */
public class 旧 {
    public static void main(String[] args) throws ParseException {
        // Date本质是一个时间戳
        Date date = new Date();
        System.out.println(date.getTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 格式化日期
        System.out.println(sdf.format(date));


        // 计算两个时间的差
        Date parse1 = sdf.parse("2025-6-17 20:19:70");
        Date parse2 = sdf.parse("2025-6-18 20:19:60");
        // 时间差（毫秒）
        long diffInMillis = Math.abs(parse1.getTime() - parse2.getTime());

        // 换算成其他单位
        long diffInSeconds = diffInMillis / 1000;
        long diffInMinutes = diffInSeconds / 60;
        long diffInHours   = diffInMinutes / 60;
        long diffInDays    = diffInHours / 24;

        System.out.println("时间差（毫秒）：" + diffInMillis);
        System.out.println("时间差（秒）：" + diffInSeconds);
        System.out.println("时间差（分钟）：" + diffInMinutes);
        System.out.println("时间差（小时）：" + diffInHours);
        System.out.println("时间差（天）：" + diffInDays);

        // 更精确的时间差（比如：几天几小时几分钟几秒）
        long days = diffInDays;
        long hours = diffInHours % 24;
        long minutes = diffInMinutes % 60;
        long seconds = diffInSeconds % 60;

        System.out.printf("时间差为：%d 天 %d 小时 %d 分钟 %d 秒%n", days, hours, minutes, seconds);
    }
}
